import { BrowserWindow } from 'electron'
import eventRouter from "../../router/EventRouter"

export default class BaseWindow {
  protected frame: BrowserWindow | null = null

  protected ensureFrame(): BrowserWindow {
    if (!this.frame) {
      throw new Error(`${this.constructor.name} 的 frame 还没有创建，请先调用 create()`)
    }
    return this.frame
  }

  getWindow(key: string) {
    return eventRouter.getApi(key)
  }

  //
  minimize(): void { // 最小化
    this.ensureFrame().minimize()
  }
  maximize(): void { // 最大化
    this.ensureFrame().maximize()
  }
  restore(): void { // 恢复
    this.ensureFrame().restore()
  }
  isMaximized(): boolean { // 是否最大化
    return this.ensureFrame().isMaximized() ?? false
  }
  isFullScreen(): boolean { // 是否全屏
    return this.ensureFrame().isFullScreen() ?? false
  }
  setFullScreen(bool: boolean): void {// 全屏
    this.ensureFrame().setFullScreen(bool)
  }
  setSize(w: number, h: number): void { // 设置大小
    this.ensureFrame().setSize(w, h)
  }
  center(): void { // 居中
    this.ensureFrame().center()
  }

  setResizable(bool: boolean): void { // 是否可调整大小
    this.ensureFrame().setResizable(bool)
  }

  setAlwaysOnTop(bool: boolean): void {
    this.ensureFrame().setAlwaysOnTop(bool)
  }

  setMinimumSize(w: number, h: number): void {
    this.ensureFrame().setMinimumSize(w, h)
  }

  isDestroyed(): boolean {
    return this.ensureFrame().isDestroyed() ?? true
  }

  destroy(): void {
    this.ensureFrame().destroy()
  }

  show(): void {
    this.ensureFrame().show()
  }

  hide(): void {
    this.ensureFrame().hide()
  }

  close(): void {
    this.ensureFrame().close()
  }

  getPosition(): number[] {
    return this.ensureFrame().getPosition() ?? [0, 0]
  }

  getBounds(): Electron.Rectangle {
    return this.ensureFrame().getBounds() ?? { x: 0, y: 0, width: 0, height: 0 }
  }

  setPosition(x: number, y: number): void {
    this.ensureFrame().setPosition(x, y)
  }

  setBounds(x: number, y: number, width: number, height: number): void {
    this.ensureFrame().setBounds({ x, y, width, height }, false)
  }

  setIgnoreMouseEvents(bool: boolean): void {
    this.ensureFrame().setIgnoreMouseEvents(bool, { forward: true })
  }

  isVisible(): boolean {
    return this.ensureFrame().isVisible() ?? false
  }
  focus(): void {
    this.ensureFrame().focus()
  }
}
